package com.senyin.dao;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.senyin.common.BaseResponse;
import com.senyin.common.ErrorCode;
import com.senyin.common.ResultUtils;
import com.senyin.entity.Dept;
import com.senyin.entity.StaffInfo;
import com.senyin.utils.JDBCUtil;
import com.senyin.utils.UUIDUtil;
import org.junit.jupiter.api.Test;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.senyin.constant.UserConstant.DEFAULT_ROLE;
import static com.senyin.constant.UserConstant.USER_LOGIN_STATE;

public class DeptDao {
    public BaseResponse<List<Dept>> showAll() throws SQLException {
        Connection conn = JDBCUtil.getConn();
        Statement statement = conn.createStatement();
        String sql = "select * from dept";
        ResultSet resultSet = statement.executeQuery(sql);
        ArrayList<Dept> depts = new ArrayList<>();
        while (resultSet.next()){
            Dept dept = new Dept();
            dept.setDpId(resultSet.getString("dp_id"));
            dept.setDpName(resultSet.getString("dp_name"));
            dept.setDpDes(resultSet.getString("dp_des"));
            depts.add(dept);
        }
        if (depts.size()<=0){
            return ResultUtils.error(ErrorCode.PARAMS_ERROR,"数据为空");
        }
        return ResultUtils.success(depts,depts.size());
    }

    public BaseResponse addDept(Dept dept,HttpServletRequest request) throws SQLException {
        StaffInfo attribute = (StaffInfo) request.getSession().getAttribute(USER_LOGIN_STATE);
        if (Objects.isNull(attribute)){
            return ResultUtils.error(ErrorCode.NO_AUTH,"登录过期或未登录");
        }
        if (attribute.getSiAuth()==DEFAULT_ROLE){
            return ResultUtils.error(ErrorCode.NO_AUTH,"普通用户无权限");
        }
        Connection conn = JDBCUtil.getConn();
        Statement statement = conn.createStatement();
        /**
         * 检查有无重复
         */
        String check = "select * from dept where dp_name='"+dept.getDpName()+"'";
        ResultSet resultSet = statement.executeQuery(check);
        if (resultSet.next()){
            return ResultUtils.error(ErrorCode.PARAMS_ERROR,"部门名称重复");
        }
        String sql = "insert into dept(dp_id,dp_name,dp_des) values('"
                + UUIDUtil.get8UUID() +"','"+dept.getDpName()+"','"+dept.getDpDes()+"')";
        int i = statement.executeUpdate(sql);
        if (i>0){
            return ResultUtils.success(i);
        }
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
    }

    public BaseResponse modDept(Dept dept,HttpServletRequest request) throws SQLException {
        StaffInfo attribute = (StaffInfo) request.getSession().getAttribute(USER_LOGIN_STATE);
        if (Objects.isNull(attribute)){
            return ResultUtils.error(ErrorCode.NO_AUTH,"登录过期或未登录");
        }
        if (attribute.getSiAuth()==DEFAULT_ROLE){
            return ResultUtils.error(ErrorCode.NO_AUTH,"普通用户无权限");
        }
        Connection conn = JDBCUtil.getConn();
        Statement statement = conn.createStatement();
        String check = "select * from dept where dp_name='"+dept.getDpName()
                +"' and dp_id!='"+dept.getDpId()+"'";
        if(statement.executeQuery(check).next()){
            JDBCUtil.close(conn,statement,null);
            return ResultUtils.error(ErrorCode.PARAMS_ERROR,"部门名称重复");
        }
        String sql = "update dept set dp_name='"+dept.getDpName()
                +"',dp_des='"+dept.getDpDes()
                +"' where dp_id='"+dept.getDpId()+"'";
        int i = statement.executeUpdate(sql);
        JDBCUtil.close(conn,statement,null);
        if (i>0){
            return ResultUtils.success(i);
        }
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
    }

    public BaseResponse delDeptByUUID(String UUID,HttpServletRequest request) throws SQLException {
        StaffInfo attribute = (StaffInfo) request.getSession().getAttribute(USER_LOGIN_STATE);
        if (Objects.isNull(attribute)){
            return ResultUtils.error(ErrorCode.NO_AUTH,"登录过期或未登录");
        }
        if (attribute.getSiAuth()==DEFAULT_ROLE){
            return ResultUtils.error(ErrorCode.NO_AUTH,"普通用户无权限");
        }
        Connection conn = JDBCUtil.getConn();
        Statement statement = conn.createStatement();
        String sql = "delete from dept where dp_id='"+UUID+"'";
        int i = statement.executeUpdate(sql);
        if (i>0){
            return ResultUtils.success(i);
        }
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
    }

    @Test
    public void testAdd() throws SQLException {
        Dept dept = new Dept("财务部", "无");
        BaseResponse baseResponse = addDept(dept, null);
        System.out.println(baseResponse.getDescription());
    }
}
